﻿using Learn.Repository.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Learn.Repository.Instance
{
    /// <summary>
    /// MYSQL 仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MySql_Repository<T> : I_Repository<T> where T : class, new()
    {
        private readonly DbContext _dbContext;

        public MySql_Repository(DbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            _dbContext.Set<T>().Add(t);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="ie"></param>
        public void AddRange(IEnumerable<T> ie)
        {
            _dbContext.Set<T>().AddRange(ie);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        public void Delete(T t)
        {
            _dbContext.Set<T>().Remove(t);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ie"></param>
        public void DeleteRange(IEnumerable<T> ie)
        {
            _dbContext.Set<T>().RemoveRange(ie);
        }

        /// <summary>
        /// 主键查询
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public T Find(params object[] keys)
        {
            return _dbContext.Set<T>().Find(keys);
        }

        /// <summary>
        /// 通用查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>> where = null)
        {

            var iq = (IQueryable<T>)_dbContext.Set<T>();
            if (where != null)
            {
                iq = iq.Where(where);
            }
            return iq;
        }

        /// <summary>
        /// 查询排序
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="isAsc"></param>
        /// <param name="order"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>> where = null)
        {
            var iq = this.QueryAll(where);
            return isAsc ? iq.OrderBy(order) : iq.OrderByDescending(order);
        }

        /// <summary>
        /// 查询、排序、分页
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="total"></param>
        /// <param name="skip"></param>
        /// <param name="limit"></param>
        /// <param name="isAsc"></param>
        /// <param name="order"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(out int total, int skip, int limit, bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>> where = null)
        {
            var iq = this.QueryAll( isAsc,order,where);
            total = iq.Count();
            return iq.Skip(skip).Take(limit);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return _dbContext.SaveChanges();

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(T t)
        {
            _dbContext.Entry<T>(t).State = EntityState.Modified;

        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateRange(IEnumerable<T> ie)
        {
            foreach (var item in ie)
            {
                _dbContext.Entry<T>(item).State = EntityState.Modified;
            }
        }
    }
}
